package com.atguigu.tingshu.account.service.impl;

import com.atguigu.tingshu.common.execption.TingShuException;
import com.atguigu.tingshu.vo.account.AccountLockVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.atguigu.tingshu.account.mapper.UserAccountDetailMapper;
import com.atguigu.tingshu.account.mapper.UserAccountMapper;
import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.account.UserAccount;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements UserAccountService {

	@Autowired
	private UserAccountMapper userAccountMapper;
	
	@Autowired
	private UserAccountDetailMapper userAccountDetailMapper;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveUserAccount(Long userId) {
		UserAccount userAccount = new UserAccount();
		userAccount.setUserId(userId);
		userAccount.setTotalAmount(new BigDecimal(100));
		userAccount.setAvailableAmount(new BigDecimal(100));
		userAccount.setTotalIncomeAmount(new BigDecimal(100));
		this.save(userAccount);
		//记录用户账户明细
		this.saveUserAccountDetail(userId,"初始化用户，赠送金额100", SystemConstant.ACCOUNT_TRADE_TYPE_DEPOSIT,new BigDecimal(100),null);
	}

	@Override
	public BigDecimal getAvailableAmount(Long userId) {
		UserAccount userAccount = this.lambdaQuery().eq(UserAccount::getUserId, userId).one();
		if(userAccount != null){
			return userAccount.getAvailableAmount();
		}else{
			return null;
		}

	}

	/**
	 * 扣减用户余额
	 * @param accountLockVo
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void checkAndLock(AccountLockVo accountLockVo) {
		//扣减账户余额
		int count = baseMapper.updateAccount(accountLockVo.getAmount(),accountLockVo.getUserId());
		if(count != 1){
			throw new TingShuException(400,"扣减用户余额失败");
		}
		//保存用余额明细
		this.saveUserAccountDetail(accountLockVo.getUserId(),
				accountLockVo.getContent(),
				SystemConstant.ACCOUNT_TRADE_TYPE_MINUS,
				accountLockVo.getAmount(),
				accountLockVo.getOrderNo());
	}

	@Override
	public Page<UserAccountDetail> findUserRechargePage(Page<UserAccountDetail> pageInfo, Long userId,String type) {
		return userAccountDetailMapper.findUserRechargePage(pageInfo,userId,type);
	}

	@Override
	public Page<UserAccountDetail> findUserConsumePage(Page<UserAccountDetail> pageInfo, Long userId, String type) {
		return userAccountDetailMapper.findUserRechargePage(pageInfo,userId,type);
	}

	/**
	 * 记录用户账户明细
	 * @param userId
	 * @param title
	 * @param tradeType
	 * @param amount
	 * @param order
	 */
	private void saveUserAccountDetail(Long userId, String title, String tradeType, BigDecimal amount, String orderNo) {
		UserAccountDetail userAccountDetail = new UserAccountDetail();
		userAccountDetail.setUserId(userId);
		userAccountDetail.setTitle(title);
		userAccountDetail.setTradeType(tradeType);
		userAccountDetail.setAmount(amount);
		userAccountDetail.setOrderNo(orderNo);
		userAccountDetailMapper.insert(userAccountDetail);
	}
}
